package com.cs.manaer.service;

import com.cs.manaer.common.utils.RedisSerializableUtils;
import com.cs.manaer.controller.req.ListSystemNotifyReqMsg;
import com.cs.manaer.controller.req.SystemNotifyReqMsg;
import com.cs.manaer.controller.res.ListSystemNotifyResMsg;
import com.cs.manaer.dao.SystemNotifyMapper;
import com.cs.manaer.po.SystemNotify;
import com.lin.common.constant.CommonConstant;
import com.lin.common.utils.BeanCopyUtils;
import com.lin.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.validation.Valid;

@Component
public class SystemNotifyServiceImpl implements SystemNotifyService {
    private static final Logger log = LoggerFactory.getLogger(SystemNotifyServiceImpl.class);
    @Autowired
    private SystemNotifyMapper systemNotifyMapper;

    @Override
    public Integer publishSystemNotify(Long id) throws Exception {
        SystemNotify sn = systemNotifyMapper.selectByPrimaryKey(id);
        sn.setStatus("1");
        return systemNotifyMapper.updateByPrimaryKeySelective(sn);
    }

    @Override
    public ListSystemNotifyResMsg listSystemNotify(@Valid ListSystemNotifyReqMsg reqMsg) {
        ListSystemNotifyResMsg lsn = new ListSystemNotifyResMsg();
        SystemNotify sn = BeanCopyUtils.beanCopy(reqMsg, SystemNotify.class);
        lsn.setRecords(systemNotifyMapper.listnotify(sn));
        lsn.setTotal(systemNotifyMapper.listnotifycount());
        return lsn;
    }

    @Override
    public Integer addSystemNotify(@Valid SystemNotifyReqMsg reqMsg) throws Exception {
        SystemNotify sn = BeanCopyUtils.beanCopy(reqMsg, SystemNotify.class);
        sn.setCreateTime(DateUtils.fullDateFormat());
        sn.setUpdateTime(DateUtils.fullDateFormat());
        sn.setStatus("0");
        sn.setId(Long.parseLong(RedisSerializableUtils.generateForLong(CommonConstant.DEFAULT_LENGTH)));
        return systemNotifyMapper.insertSelective(sn);
    }

    @Override
    public Integer delSystemNotify(Long id) throws Exception {
        return systemNotifyMapper.deleteByPrimaryKey(id);
    }
}
